#ifndef COMMON_TYPE_H
#define COMMON_TYPE_H

#include <stdint.h>
#include <stddef.h>

/* VERSION */
#define TIMESTAMPER_VER ("Build Time: " __DATE__ " " __TIME__)

/* ERRCODE */
typedef enum {
    ERR_SUCCESS = 0,
    ERR_FAILURE = 1,
    ERR_BADPARA = 2,
    ERR_MATHERR = 2,
    ERR_TIMEOUT = 3,
    ERR_FRMEERR = 4,
    ERR_CMDNREG = 5,
    ERR_TASKBSY = 6,
    ERR_ASSERT = 7,
    ERR_ACKMISS = 8,
    ERR_DEVNEXIST = 9,
    ERR_BINDERR = 10,
    ERR_VIRTFUNC = 11,
    FRM_OK = 0xf0, // 包长度传输匹配
    FRM_RCCERR, // 包校验和有误
    FRM_ONGOING, // 包长度不足
    FRM_NULL, // 输入空指针
    FRM_HEADERR, // 帧头有误
    FRM_LENERR, // 包长度有误
    FRM_CIONRDY // 环形队列未就绪
} ERRCODE;

/* OBJ STA */
typedef enum {
    DRV_INVALID = 0,
    DRV_VALID = 1,
} OBJSTA;

/* 线性校准对象 */
#define ALGO_LINEAR_CALIB_PTSCNT 9
typedef struct
{
    int32_t x;
    int32_t y;
} dataPt;
typedef struct algoLinearCalib
{
    uint32_t tableID; // 参数表ID
    int32_t xUnit; // x轴单位 = 1*10^xUnit
    int32_t yUnit; // y轴单位 = 1*10^yUnit
    dataPt pts[ALGO_LINEAR_CALIB_PTSCNT]; // 校准点系
    uint8_t res[40]; // 保留字节
    uint32_t crc; // 参数表校验和
} algoLinearCalib;

typedef enum RadioType
{
    RadioType5832Tx,
    RadioType5808Rx,
} RadioType;

typedef struct RadioChannel
{
    int32_t idx;
    int32_t freq; // in Hz
    uint32_t bandwidth; // in Hz
    uint32_t rssi; // in 100%
	char chName;
	char freqName;
} RadioChannel;
extern const char RadioTypeName[][32];
extern const int FreqPointTableRx5808[];

/* 射频通信模块对象 */
typedef struct pkgDataRssi
{
    uint32_t reserved[4];
    uint32_t channelCnt;
    uint16_t rssi[14];
} pkgDataRssi;

// 设备开工
typedef enum {
    DEV_ARMED = 1,
    DEV_STOP,
} DEV_ARM_STA; // 是否启动输出

// 上下电
typedef enum {
    DEV_PWROFF = 0,
    DEV_PWRON,
} DEV_POWER_STA;

// 循环标志位
typedef enum {
    WORKMODE_LOOP = 1,
    WORKMODE_SINGLE,
    WORKMODE_MAX,
} DEV_WORKMODE;
#define INVALID_PARA 0xFFFFFFFF

/* DEVICE TYPE */
#define DEVTYPENAME_CALIBER "DEVCALIB"
typedef enum {
    DEVTYPE_INVALID,
    DEVTYPE_MCPS,
    DEVTYPE_MCPSV2Z,
    DEVTYPE_CALIBER,
    DEVTYPE_MEASURE,
    DEVTYPE_MCPS6400,
} DEV_TYPE;

/* UI刷新率 */
#define UI_FPS 30.0
#define UI_PERIOD (1000.0/UI_FPS)

/* 公共方法 */
#define DISCARD(x) (void)(x)

/* 连接方式枚举 */
typedef enum {
    LINK_TOGGLE,
    LINK_OPEN,
    LINK_CLOSE,
} LINK_CMD_TYPE;

/* 设备超时常数，单位s */
#define DEV_OFFLINE_TIMEOUT 0.3

typedef enum {
    STEPPER_SPEEDLOCK,
    STEPPER_POSLOCK,
    STEPPER_STOP,
} STEPPER_MODE;

typedef enum {
    AXIS_X,
    AXIS_Y,
} AXIS_TYPE;

#define CLAMP_VALUE(x,min,max) if ((x) < (min))  (x) = (min); if ((x) > (max)) (x) = (max)

typedef enum {
    GPIO_CLR,
    GPIO_SET,
} GPIO_STA;

typedef enum {
    LcdRotate0,
    LcdRotate90,
    LcdRotate180,
    LcdRotate270,
} LcdRotate;

struct LcdSt7789
{
    uint32_t lcdWidth, lcdHeight;
    uint32_t colorBack, colorFront, colorL1, colorL2;
    uint16_t* GRAM;
    uint32_t GramSize;
    LcdRotate rotate;
};

/* 设备上报事件 */
typedef enum _EventTypeID {
    EVTID_ETH_LINKUP,
    EVTID_ETH_LINKDOWN,
} EventTypeID;

typedef enum {
    MCPS_CS_INVALID,
    MCPS_CS_138, // 通过3-8译码器控制片选
    MCPS_CS_PCA, // 通过PCA9555控制片选
} MCPS_CS_SOLUTION;

/* 设备状态 */
typedef enum _devSta {
    DEVSTA_BOOT,
    DEVSTA_ETH_ONLINE,
    DEVSTA_ETH_OFFLINE,
} devSta;

typedef enum _CHANNEL_x {
    CHNL_1,
    CHNL_2,
    CHNL_3,
    CHNL_4,
    CHNL_5,
    CHNL_6,
    CHNL_7,
    CHNL_8,
    CHNL_9,
    CHNL_10,
    CHNL_11,
    CHNL_12,
    CHNL_13,
    CHNL_14,
    CHNL_15,
    CHNL_16,
} CHANNEL_x;

#endif // COMMON_TYPE_H
